package edu.hawaii.webspider;

import com.meterware.httpunit.GetMethodWebRequest;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;
import java.io.IOException;
import java.net.MalformedURLException;
import org.xml.sax.SAXException;
import static java.lang.System.out;
import java.util.logging.Logger;
import java.util.logging.Level;
/**
 * A class to count the total links on a page.
 * 
 * @author Kevin English (with support from Phillip Johnson)
 */
public class TotalLinks extends WebAnalysisTool {

  private int pagesToVisit = 100;

  /**
   * Public contructor.
   *
   */
  public TotalLinks() {
    super();
  }
  /**
   * Public constructor with url parameter.
   * @param url The url where web crawling will begin.
   */
  public TotalLinks(String url) {
    super(url);
  }

  /**
   * Returns the number of links found at the startUrl.
   * 
   * @return The number of links.
   */
  protected boolean isStopCondition() {
    return webResponses.size() >= this.pagesToVisit;
  }
  
  /**
   * Main method, returns the number of links found. 
   * @param count Number of pages to crawl.
   * @return The number of links.
   */
  public int getLinks(int count) {
    // create the conversation object which will maintain state for us
    this.pagesToVisit = count;

    if (!isValidHostUrl(this.startUrl)) {
      return 0;
    }

    WebRequest request = new GetMethodWebRequest(this.startUrl);
    WebResponse response;
    int total = 0;
    try {
      response = webConversation.getResponse(request);
      this.populateWebResponses(response);

      for (String url : webResponses.keySet()) {
        logger.info("Retrieved " + url);
        logger.info("Found " + webResponses.get(url).getLinks().length + " Links.");        
        total += webResponses.get(url).getLinks().length;
      }
    }
    catch (MalformedURLException e) {

      return 0;
    }
    catch (IOException e) {
      return 0;
    }
    catch (SAXException e) {
      return 0;
    }
    System.out.println("The total number of links discovered while crawling the first "
        + this.pagesToVisit + " pages accessable from " + getStartUrl() + "  is: " + total);
    return total;

  }

  /**
   * Retrieves the httpunit home page and counts the number of links on it.
   * @param argv command line arguments.
   */
  public static void main(String[] argv) {
     Logger.getLogger("edu.hawaii.webspider").setLevel(Level.INFO);
    
    String url = "http://www.hackystat.org";
    TotalLinks example = new TotalLinks(url);
    out.println("The TotalLinks home page contains " + example.getLinks(100) + " links.");
  }
}
